﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.IO;
using System.Web;
using Microsoft.AspNetCore.Http;
using System.Text;

namespace QualitySupervision.Utility
{
    public class FilePathHelper
    {
        /// <summary>
        /// 创建文件虚拟目录路径
        /// </summary>
        /// <param name="virtualDirName"></param>
        /// <returns></returns>
        public static string GenerateFileVirtualPath(string virtualDirName = "filedata")
        {
            var phicyPath = Path.Combine(virtualDirName,
                                         DateTime.Now.Year.ToString(),
                                         DateTime.Now.Month.ToString(),
                                         DateTime.Now.Day.ToString(),
                                         DateTime.Now.ToString("HHmm"));
            return phicyPath;
        }



        /// <summary>
        /// 创建文件存储路径(包含根目录及文件存储文件夹 )
        /// </summary>
        /// <param name="rootPath"></param>
        /// <param name="virtualFilePath"></param>
        /// <returns></returns>
        public static string GenerateWWWRootFileFullPath(string rootPath, string virtualFilePath)
        {
            if (string.IsNullOrEmpty(rootPath)) rootPath = "wwwroot";//自动创建wwwroot目录
            var phicyPath = Path.Combine(rootPath, virtualFilePath);
            Directory.CreateDirectory(phicyPath);
            return phicyPath;
        }

        /// <summary>
        /// 创建文件存储路径
        /// </summary>
        /// <param name="rootPath"></param>
        /// <param name="virtualFilePath"></param>
        /// <returns></returns>
        public static string GenerateWWWRootFileFullPath2(string rootPath, string virtualFilePath)
        {
            if (string.IsNullOrEmpty(rootPath)) rootPath = "wwwroot";//自动创建wwwroot目录
            var phicyPath = Path.Combine(rootPath, virtualFilePath);
            return phicyPath;
        }

        /// <summary>
        /// 创建分布式文件存储路径
        /// </summary>
        /// <param name="rootPath"></param>
        /// <param name="virtualFilePaths"></param>
        /// <returns></returns>
        public static string GenerateWWWRootFileFullPath(string rootPath, string[] virtualFilePaths)
        {
            var phicyPath = Path.Combine(rootPath, virtualFilePaths[new Random().Next(0, 1)]);
            Directory.CreateDirectory(phicyPath);
            return phicyPath;
        }

        /// <summary>
        /// 创建唯一文件名
        /// </summary>
        /// <param name="extension">支持带.扩展名，eg:.jpg or jpg</param>
        /// <returns></returns>
        public static string GenerateUniqueFileName(string extension = "")
        {
            return string.Concat(Path.GetRandomFileName().Replace(".", ""), (!string.IsNullOrEmpty(extension)) ? (extension.StartsWith(".") ? extension : string.Concat(".", extension)) : "");
        }
    }
}
